/**
 * 鱼塘中的鱼 没有直接用钓鱼里面的鱼
 */
class SmallFish extends BaseComp{
    private img_fish:eui.Image;
    private tips_group:eui.Group;
    private lb_money:eui.Label;
    private fishData;
    private fishId:number;
    private fishSpeed:number;
    public dir:number;
    private money_group:eui.Group;
    private lb_tips:eui.Label;

    constructor(data?)
    {
        super('SmallFishView',data);
        this.fishId = this.data.fish_id;
    }

    public init()
    {
        this.cacheAsBitmap = true;

        this.fishData = FishTpl.instance().getDataById(this.fishId);
        if(this.fishData)
        {
            this.fishSpeed = FishTpl.instance().getSpeedByQuilty(this.fishData['quilty']) * Math.random() * 3;
            this.addFish();
        }
        else
        {
            LogUtil.error("FishId don't exit in the fishTpl=="+this.fishId);
        }
    }

        //添加鱼主体
    protected addFish()
    {
        this.dir = Math.floor(Math.random()*2);
        this.img_fish.texture = RES.getRes(this.fishData['icon']);
        this.width = this.img_fish.width;
        this.height = this.img_fish.height;

        if(this.dir == DirType.right)
        {
            this.img_fish.scaleX = -1;
        }
    }

    public getSpeed()
    {
        return this.fishSpeed;
    }

    //改变鱼的方向
    public changeFishDir()
    {
        if(this.img_fish)
        {
            this.dir = this.dir == DirType.left ? DirType.right : DirType.left;
            this.img_fish.scaleX *= -1;
            this.fishSpeed = FishTpl.instance().getSpeedByQuilty(this.fishData['quilty']) * Math.random() * 2;
        }
    }

    public addEvent()
    {
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.afterTouch, this);
    }

    protected afterTouch()
    {
        let random = Math.random();
        if(random > 0.5)
        {
            if(random > 0.8)
            this.showScared();
            else
            this.fishSpeed = 6;

            this.tips_group.visible = true;
            this.money_group.visible = false;
            this.lb_tips.visible = true;
            egret.setTimeout(()=>{
                this.tips_group.visible = false;
            },this, 2000);
        }
        else
        {
            FishPoolsPrase.instance().soleFish(this.data, this.soleSuccess, this);
        }
    }
    
    public soleSuccess()
    {
        if(this.parent)
        {
            this.AddIconEffect();
            this.remove();
        }
        let logic:FishPoolsUI = UIManager.instance().getLogicByName(UIConst.FISH_POOLS) as FishPoolsUI;
        if(logic)
        {
            logic.updateFishCnt();
        }
    }

    public AddIconEffect()
    {
        let texture = RES.getRes('gold_fish_png');
        let coin = new eui.Image(texture);
        coin.anchorOffsetX = coin.width >> 1;
        coin.anchorOffsetY = coin.height >> 1;
        let worldPoint = this.parent.localToGlobal(this.x, this.y);
        coin.x = worldPoint.x;
        coin.y = worldPoint.y;
        this.parent.parent.addChild(coin);
        EffectUtil.instance().RotationTree(coin);
        egret.Tween.get(coin).to({
            y:coin.y - 100,
            alpha:0
        },500).call(()=>{
            EffectUtil.instance().StopEffect(coin);
            coin.parent.removeChild(coin);
        })
    }

    public showScared()
    {
        let dir = this.dir == DirType.left ? -1 : 1;
        egret.Tween.get(this).to({
            x:this.x + dir * 50,
            y:this.y + dir * 30
        }, 200, egret.Ease.sineOut);
    }

    public showTips()
    {
        this.tips_group.visible = true;
        if(this.data.income && this.data.income != 0)
        {
            this.lb_money.text = this.data.income + '';
        }
        else
        {
            this.lb_money.text = '别急哦~';
        }
        this.money_group.visible = true;
        this.lb_tips.visible = false;
        EffectUtil.instance().flyObj(this.tips_group,500,15);
        egret.setTimeout(()=>{
            EffectUtil.instance().StopEffect(this.tips_group);
            this.tips_group.visible = false;
        },this, 2000);
    }

    public removeEvent()
    {
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.afterTouch, this);
    }

    public clear()
    {
        super.clear();
        egret.Tween.removeTweens(this);
        EffectUtil.instance().StopEffect(this.tips_group);
        this.fishSpeed = null;
        this.fishData = null;
        this.fishId = null;
        this.dir = null;
    }
}